/*
 * Created by JFormDesigner on Wed Aug 13 21:14:33 CST 2025
 */

package org.xl.main;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author Windows
 */
public class LoginForm extends JFrame {
    public LoginForm() {
        initComponents();
    }

    private void login(ActionEvent e) {
        // TODO add your code here
        login();
    }

    private void login() {
        String userName = textField1.getText();
        String password = passwordField1.getText();
        if (userName.equals("admin") && new String(password).equals("admin")) {
            JOptionPane.showMessageDialog(this,"登录成功");
            this.dispose();
            //打开选择页
            SwingUtilities.invokeLater(() -> {
                MainItemFormS mainItemFormS = new MainItemFormS();
                mainItemFormS.setSize(1200,800);
                mainItemFormS.setVisible(true);
                mainItemFormS.setDefaultCloseOperation(EXIT_ON_CLOSE);
            });
        } else  {
            JOptionPane.showMessageDialog(this,"用户密码错误,登录失败");
        }
    }

    private void passwordField1KeyPressed(KeyEvent e) {
        // TODO add your code here
        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
            login();
        }
    }



    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        textField1 = new JTextField();
        label1 = new JLabel();
        label2 = new JLabel();
        button1 = new JButton();
        passwordField1 = new JPasswordField();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- textField1 ----
        textField1.setText("admin");
        textField1.setEditable(false);
        contentPane.add(textField1);
        textField1.setBounds(200, 60, 155, textField1.getPreferredSize().height);

        //---- label1 ----
        label1.setText("\u7528\u6237\u540d:");
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(150, 65), label1.getPreferredSize()));

        //---- label2 ----
        label2.setText("\u5bc6   \u7801:");
        contentPane.add(label2);
        label2.setBounds(150, 110, 39, 17);

        //---- button1 ----
        button1.setText("\u767b\u5f55");
        button1.addActionListener(e -> login(e));
        contentPane.add(button1);
        button1.setBounds(new Rectangle(new Point(215, 155), button1.getPreferredSize()));

        //---- passwordField1 ----
        passwordField1.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                passwordField1KeyPressed(e);
            }
        });
        contentPane.add(passwordField1);
        passwordField1.setBounds(200, 105, 155, passwordField1.getPreferredSize().height);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JTextField textField1;
    private JLabel label1;
    private JLabel label2;
    private JButton button1;
    private JPasswordField passwordField1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
